package cc.yugu.yhdw.common.tool.memcached;


import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import org.springframework.beans.factory.FactoryBean;

public class MemCachedClientFactory implements FactoryBean<MemCachedClient> {

    MemCachedConfig config;

    @Override
    public MemCachedClient getObject() throws Exception {
        return build(config);
    }

    @Override
    public Class<?> getObjectType() {
        return MemCachedClient.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public MemCachedConfig getConfig() {
        return config;
    }

    public void setConfig(MemCachedConfig config) {
        this.config = config;
    }

    public static MemCachedClient build(MemCachedConfig config) {
        SockIOPool pool = SockIOPool.getInstance();
        pool.setServers(config.servers);
        pool.setWeights(config.weights);

        pool.setNagle(config.nagleAlg);
        pool.setSocketTO(config.socketTimeOut);
        pool.setSocketConnectTO(config.socketConnectTimeout);

        pool.setInitConn(config.initialConnections);
        pool.setMinConn(config.minSpareConnections);
        pool.setMaxConn(config.maxSpareConnections);
        pool.setMaxIdle(config.maxIdleTime);
        pool.setMaintSleep(config.maintThreadSleep);
        pool.setMaxBusyTime(config.maxBusyTime);

        pool.setMaintSleep(config.maintThreadSleep);
        pool.setSocketTO(config.socketTimeOut);
        pool.setNagle(config.nagleAlg);
        pool.setHashingAlg(config.hashingAlg);
        pool.setAliveCheck(config.aliveCheck);

        pool.initialize();

        MemCachedClient client = new MemCachedClient();
        return client;
    }



}
